package igxe

import (
	"learn/smart/config"
	"learn/smart/fetcher"
	"learn/smart/parser/igxe/login"
	"learn/smart/parser/igxe/sell"
	"time"
)

type Worker struct {
	User    *config.User
	fetcher *fetcher.Fetcher
	sell    sell.Sell
	//TODO PURCHASE
}

// ready to do work
func (w *Worker) ready() {
	w.userLogin()
	w.initSell()
}

// start login igxe
func (w *Worker) userLogin() {
	f := &fetcher.Fetcher{BaseUrl: "https://www.igxe.cn"}
	l := login.InitLogin(f, w.User)
	l.Do()
	w.fetcher = f
}

//每过一个小时重新登录一次
func (w *Worker) reLogin() {
	go func() {
		for {
			time.Sleep(time.Hour)
			w.userLogin()
		}
	}()
}

func (w *Worker) initSell() {
	w.sell = sell.Sell{Fetcher: w.fetcher, User: w.User}
	w.sell.Init()
}

func (w *Worker) Do() {
	w.ready()
	// 自动重新登录worker
	w.reLogin()
	// 出售worker
	w.sell.Start()
}
